package com.markyao.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.markyao.dao.mapper.ProductMapper;
import com.markyao.dao.pojo.Product;
import com.markyao.miaosha.MiaoshaProduct;
import com.markyao.service.CartService;
import com.markyao.service.CategoryService;
import com.markyao.service.ProductService;
import com.markyao.utils.DateFormat;
import com.markyao.vo.Result;
import com.markyao.vo.SearchVo;
import com.markyao.vo.parmas.CartParmas;
import com.markyao.vo.parmas.SearchParma;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;


    @Override
    public Result findHotList() {
        //查找有优先级前四的商品列表。
        List<Product>hotList=productMapper.findHotList();
        return new Result(true,"查找成功",000,hotList);
    }

    @Override
    public Result findDetailsById(Integer id) {
        //通过id查找商品详情
        Product product=productMapper.findById(id);
        Date miaoshaEnd = product.getMiaoshaEnd();
        Date miaoshaStart = product.getMiaoshaStart();
        if (miaoshaEnd !=null&& miaoshaStart !=null){
            //处理一下日期格式
            MiaoshaProduct productVo=copy(product);
            return Result.success(000,"查找成功",productVo);
        }
        return Result.success(000,"查找成功",product);
    }

    private MiaoshaProduct copy(Product product) {
        MiaoshaProduct miaoshaProduct=new MiaoshaProduct();
        BeanUtils.copyProperties(product,miaoshaProduct);
        miaoshaProduct.setMiaoshaStart(DateFormat.toStringDate(product.getMiaoshaStart()));
        miaoshaProduct.setMiaoshaEnd(DateFormat.toStringDate(product.getMiaoshaEnd()));
        return miaoshaProduct;
    }

    @Override
    public Result findNewList() {
        //查找有优先级前四的商品列表。
        List<Product>newList=productMapper.findNewList();
        return new Result(true,"查找成功",000,newList);
    }

    @Autowired
    CartService cartService;
    @Override
    public Result addCart(CartParmas cartParmas) {
        Integer num = cartParmas.getNum();
        double price = Double.parseDouble(cartParmas.getPrice());
        double sum=num*price;
        cartParmas.setPrice(String.valueOf(sum));
        cartService.insertCart(cartParmas);
        return new Result(true,"加入购物车成功",111,null);
    }

    @Autowired
    CategoryService categoryService;
    @Override
    public Result getPageList(SearchParma searchParma) {
        PageHelper.startPage(searchParma.getCurrentPage(),12);
        List<Product>productList=new ArrayList<>();
        SearchVo searchVo=new SearchVo();
        if(searchParma.getCID()!=null ){
            productList=productMapper.findByCid(searchParma.getCID());
            String cName=categoryService.getNameByCid(searchParma.getCID());
            searchVo.setSearch(cName);
        }
        else{
            productList=productMapper.findBySearch(searchParma.getSearch());
            searchVo.setSearch(searchParma.getSearch());
        }

        PageInfo pageInfo=new PageInfo(productList);
        System.out.println("-------------"+productList.size());
        Integer total = pageInfo.getPages();
        System.out.println(total);
        searchVo.setPageList(productList);
        searchVo.setCurrentPage(searchParma.getCurrentPage());
        searchVo.setTotalPage(total);

        return new Result(true,"查询成功",111,searchVo);
    }

    @Override
    public Product getProductByid(Integer goodsId) {
        return productMapper.findById(goodsId);
    }

    @Override
    public int reduceMiaoshaCnt(Integer goodsId) {
        return productMapper.reduceMiaoshaCnt(goodsId);

    }


}
